package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Proxy;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 上午11:03
 */
@Entity
@Table(name = "token")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update token set token_is_deleted = true where token_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "token_is_deleted = false")
@ApiModel("token")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
@EntityListeners(AuditingEntityListener.class)
@Proxy(lazy = false)
public class Token extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId", parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "TOKEN")})
    @Column(name = "token_id")
    @ApiModelProperty(value = "token的id", position = 1)
    private String tokenId;

    @Column(name = "token_effective_date")
    @ApiModelProperty(value = "token生效时间", position = 2)
    @CreatedDate
    private Date tokenEffectiveDate;

    @Column(name = "token_expiry_date")
    @ApiModelProperty(value = "token失效时间", position = 3)
    private Date tokenExpiryDate;

    @Column(name = "token_token")
    @ApiModelProperty(value = "token", position = 3)
    private String tokenToken;

    @OneToOne(targetEntity = User.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "token_user_id", referencedColumnName = "user_sid")
    private User user;

    public User getUser() {
        return user;
    }

    public Token setUser(User user) {
        this.user = user;
        return this;
    }

    @Column(name = "token_is_forbidden")
    @ApiModelProperty(value = "token是否被禁止", position = 4)
    private boolean tokenIsForbidden = false;

    @Column(name = "token_is_deleted")
    @ApiModelProperty(value = "token是否被删除", position = 5)
    private boolean tokenIsDeleted = false;

    public Token(String tokenId, Date tokenEffectiveDate, Date tokenExpiryDate, String tokenToken, boolean tokenIsForbidden, boolean tokenIsDeleted, List<User> users) {
        this.tokenId = tokenId;
        this.tokenEffectiveDate = tokenEffectiveDate;
        this.tokenExpiryDate = tokenExpiryDate;
        this.tokenToken = tokenToken;
        this.tokenIsForbidden = tokenIsForbidden;
        this.tokenIsDeleted = tokenIsDeleted;
    }

    public Token() {
    }

    public static TokenBuilder builder() {
        return new TokenBuilder();
    }

    @Override
    public String toString() {
        return "Token{" + "tokenId='" + tokenId + '\'' + ", tokenEffectiveDate=" + tokenEffectiveDate + ", tokenExpiryDate=" + tokenExpiryDate + ", tokenIsForbidden=" + tokenIsForbidden + ", tokenIsDeleted=" + tokenIsDeleted + '}';
    }

    public String getTokenId() {
        return tokenId;
    }

    public Token setTokenId(String tokenId) {
        this.tokenId = tokenId;
        return this;
    }

    public Date getTokenEffectiveDate() {
        return tokenEffectiveDate;
    }

    public Token setTokenEffectiveDate(Date tokenEffectiveDate) {
        this.tokenEffectiveDate = tokenEffectiveDate;
        return this;
    }

    public Date getTokenExpiryDate() {
        return tokenExpiryDate;
    }

    public Token setTokenExpiryDate(Date tokenExpiryDate) {
        this.tokenExpiryDate = tokenExpiryDate;
        return this;
    }

    public String getTokenToken() {
        return tokenToken;
    }

    public Token setTokenToken(String tokenToken) {
        this.tokenToken = tokenToken;
        return this;
    }

    public boolean isTokenIsForbidden() {
        return tokenIsForbidden;
    }

    public Token setTokenIsForbidden(boolean tokenIsForbidden) {
        this.tokenIsForbidden = tokenIsForbidden;
        return this;
    }

    public boolean isTokenIsDeleted() {
        return tokenIsDeleted;
    }

    public Token setTokenIsDeleted(boolean tokenIsDeleted) {
        this.tokenIsDeleted = tokenIsDeleted;
        return this;
    }

    public static class TokenBuilder {
        private String tokenId;
        private Date tokenEffectiveDate;
        private Date tokenExpiryDate;
        private String tokenToken;
        private boolean tokenIsForbidden;
        private boolean tokenIsDeleted;
        private User user;
        private List<User> users;

        TokenBuilder() {
        }

        public TokenBuilder tokenId(String tokenId) {
            this.tokenId = tokenId;
            return this;
        }

        public TokenBuilder tokenEffectiveDate(Date tokenEffectiveDate) {
            this.tokenEffectiveDate = tokenEffectiveDate;
            return this;
        }

        public TokenBuilder tokenExpiryDate(Date tokenExpiryDate) {
            this.tokenExpiryDate = tokenExpiryDate;
            return this;
        }

        public TokenBuilder tokenToken(String tokenToken) {
            this.tokenToken = tokenToken;
            return this;
        }

        public TokenBuilder tokenIsForbidden(boolean tokenIsForbidden) {
            this.tokenIsForbidden = tokenIsForbidden;
            return this;
        }

        public TokenBuilder tokenIsDeleted(boolean tokenIsDeleted) {
            this.tokenIsDeleted = tokenIsDeleted;
            return this;
        }

        public TokenBuilder users(List<User> users) {
            this.users = users;
            return this;
        }

        public TokenBuilder user(User user) {
            this.user = user;
            return this;
        }

        public Token build() {
            return new Token(tokenId, tokenEffectiveDate, tokenExpiryDate, tokenToken, tokenIsForbidden, tokenIsDeleted, users);
        }

        public String toString() {
            return "Token.TokenBuilder(tokenId=" + this.tokenId + ", tokenEffectiveDate=" + this.tokenEffectiveDate + ", tokenExpiryDate=" + this.tokenExpiryDate + ", tokenToken=" + this.tokenToken + ", tokenIsForbidden=" + this.tokenIsForbidden + ", tokenIsDeleted=" + this.tokenIsDeleted + ", users=" + this.users + ")";
        }
    }
}
